-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[RFC] Pluggable USB core for AVR #3304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This really sounds good to me. I havent looked at the code yet, but I will test this for sure. Also I would provide an include "USB.h" to enable USB stuff. Then users can remove the whole USB core and save even more space if not needed and also avoid problems with the PC. You may want to have a look at my core and at the dev branches to get new Ideas possibly: |
Hi Nico,
Also, your core handles a lot of HID-based devices but it's not ready for other kind of devices (like MIDI #2943 or something else that I can't imagine at the moment 😄 ) However, if you could test the patchset it would be great, I'm only waiting for the feedback! |
Well I have a midi option that I could integrate. Its just under a dev state so I did not create a proper menu yet. Also it has a 2nd Audio device with errors under windows. I just used the arcore code for this. I know that the boards file is not perfect, but it was the only way for me to simply add a menu to the IDE. I really like your IDE to make the USB stuff more flexible. You know that this will change a lot of code. Is the Arduino Team willing to change it? Because so many people provided new features but mostly none of them were integrated yet. You are collaborator, does this mean that we can count on a PR merge if its good? You should also consider to fix this bug when you update the USB core: And you should also consider to update the bootloader fuses and watchdog behaviour in order to fully deactivate the USB-Core. Right now the bootloader starts the sketch directly. You could better to a watchdog reset and start the code independent. The difference is that all settings like the USB clock is reset. Without this fix you have to add the USB clock ISR into the core as workaround. I could explain it better if someone is willing to update the bootloader in general. This would make life much easier. I am not likely able to test your code this/next week, I am too busy, sorry. But I will have a look for sure. Edit: |
Overall, this looks like a nice implementation, using a linked list of modules and separate libraries is an elegant way to solve this.
Isn't a weak setup function completely counter to the idea of pluggable modules? Only one module can implement this setup function, otherwise you'll get linker errors (or if the modules make their implementation weak too, they'll get silently ignored). Wouldn't an init function (since the setup name seems to be used already for processing control packets) in the callbacks make sense instead? Would it make sense to write some documentation about how to use this now? Having documentation would help to understand and review the PR, and writing documentation after merging usually means it'll take months before that happens... |
What do you mean by cb variable? Can we also see u2 Series support with this update? HL2 is getting more and more popular this could be a great time to merge everything together. |
I will add comments/thoughts to the PR here (this post will be edited): The different Mouse/Keyboard/MouseAndKeyboard HID reports wont work properly under Windows I want to note. You have to use different PIDs to make the driver recognize it properly or reinstall the drivers and provide a correct tutorial somewhere. May we get some diagrams on how this works together? So others can understand better. I have to fiddle through this right now. And I have to mention that I just looked at the code in small parts. I do not understand it as a whole thing since I never intended to understand it. |
#ifdef HID_ENABLED | ||
total += HID_GetInterface(&interfaces); | ||
#ifdef PLUGGABLE_USB_ENABLED | ||
PUSB_GetInterface(&interfaces); | ||
#endif | ||
|
||
return interfaces; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns always 0. Whats the sense of that function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it does't return 0 because interfaces
is incremented by the callee (instead, the original total
variable was completely useless)
I presume this was in reply to this comment I mean that instead of:
you could do:
|
@NicoHood @matthijskooijman Thank you so much for taking time to analyse the (huge) PR. I'll try to update it following your advices as soon as I can get some spare time. About OS-specific issues, I think that Nico is our man, so any help is really appreciated! |
@matthijskooijman about the weak function, I could not imagine anything better. From my tests the (also weak) However, anyone can freely overwrite it with its own non-weak implementation (like in the MIDI+HID example). Any alternative however is very welcome 😄 |
Looking more closely, I see you are using There is a good alternative, though: Use a global object, whose constructor calls As an example of how this works, see ModuleHandler and Module in https://github.com/Pinoccio/library-pinoccio/tree/master/src/modules Those register a Module* in a linked list inside ModuleHandler, which is the equivalent of calling One caveat is that this all runs before I even think that this approach doesn't require separate libraries for every module, since the compiler can completely remove a module, including its global constructor, if it's not referenced anywhere at all. I'm not 100% sure about this, though. And in most cases, using separate libraries is sane approach regardless of this. I had a closer look at the HID libraries, and I'm not so happy with how those look. AFAICS, the Mouse and Keyboard libraries both define a few specially-named symbols, which are then used by the HID library. This of course doesn't allow more than one HID "submodule", which is why there is a KeyboardAndMouse library, which is of course fairly ugly. Ideally, an approach with callbacks similar to PUSB_AddFunction is also used for HID submodules, allowing them to be completely separate from each other. However, I'm not sure if the HID descriptors are actually easy to compose (e.g. can you just concatenate or otherwise combine the Mouse and Keyboard descriptors into something valid)? |
Now the produced binary should be comparable (in size) with the non-modular one so we can start thinking about integrating the various PR as libraries and test the results |
@ArduinoBot build this please |
Size comparison with the current code:
|
https://github.com/facchinm/Arduino/tree/testPlugUSB_PR+1803 contains a complete porting of PR #1803 adapted to the Pluggable framework |
Almost end of week update:
and the descriptor will be the combination of the submodules' descriptors. The method is not very memory efficient; however you can still write a library which plugs on PluggableUSB directly, embedding in it an HID implementation and only the functions you need.
@NicoHood @matthijskooijman @obra @follower @cmaglie @madrang @damellis @PaulStoffregen @weizenspreu and all people interested in USB on AVR, please test the latest @ArduinoBot image to make sure that nothing breaks spectacularly before merging 😁 Thank you very much! |
Oh wow. That's amazing. Thank you so, so, so much. I'm on the road this week but will test when I can.
|
@yyyc514 of course it is interrupt driven. Not sure if your idea then still makes sense. @ facchinm Also see this: Going to test this sooner or later... Sounds good at least! |
revert this commit when it's time to integrate this library
expect way more interesting user-generated libraries
huh? that was quick. I didnt even had a chance to test this yet. Not that I feel bad about it, I should really go ahead and try this now! |
I think that this is one of the most-desired patch ever (just look the number of PR/Issues), so we decided to merge it upstream to accelerate tests and adoption. On our side we tested it on everything, BTW this patch is not yet released, so we are still in time to fix bugs or (in the worst case but I hope not :)) completely revert it. |
Good idea, I think its a good idea to improve the usb stack. |
What do you mean by "quick"? This topic took now 1.5 years to come any further.
|
Laugh out loud :D Yes I know. And this PR started about a month ago and is now merged. Nevermind, its GOOD that there IS progress. |
Initial howto on writing a PluggableUSB or PluggableHID based library https://github.com/arduino/Arduino/wiki/PluggableUSB-and-PluggableHID-howto |
May you eliminate the u8 which only causes problems? Edit: Will have a loom of course ;) Lookin good. |
Any chance to see a pluggable keyboard layout? |
Well Nico, a library providing multiple layouts is now possible, you can write it by yourself and we'll be glad to include it in the library manager 😉 |
Looking at the code now, adding notes here via edit:
|
Adding the Endpoint flexible size is a bit of a mess, but if you come up with a solution I'll be very happy to review it. Also, feel free to open PRs for Keyboard and Mouse libraries modifications. |
I am currently working on a PR to backport all my HID Project fixes. Already done with the major core fixes, HID libraries will follow. The flexible EP_SIZE needs a bit more core changes, however I applied my simple fix for now in the PR (soon). What is more important is this: At the moment you set those values wrong. USB cannot be pluggable in every situation. So I dont know how to solve this the best way, but we should at least try to add a correct CDC descriptor. Let me give my fixes a last check and I'll open a new PR for this. |
Edit:
|
There we go: Edit: very nice work overall. Took some time to understand the whole thing. Still looking through the code. Just tell me how we should continue. |
@facchinm Hey, you did a lot of great work. Would you be so kind to have a look at the PR to apply my small fixes? If you disagree with the CDC Events and control line stuff I can remove it. There is another PR my mathijs I think who did something different, maybe better than that. But the rest of my PR is not much to look at but would make the core more flexible and fixes a few problems. Removed, now this PR can be used: |
This PR introduces the concept of Pluggable USB modules to help getting the best from our tiny ATmega32u4 without overhauling the core library with functions that could be useful for no more than 10% of the users.
Using this method we can plug a library using the USB core by simply including its header in the sketch, and if not used it will not occupy additional resources.
It is of course a work in progress (thus tagged as RFC), still not ported to DUE core, and VERY probably bug-prone. Also, the memory footprint is not minimized.
Useful infos
master
code (so all code from Overhaul USB HID for Leonardo and Micro #1803 can be merged in a second time)setupUSB()
weak function has been added tomain.c
, so it will execute beforesetup()
ifUSBCON
is defined.Known issues
How to try it
I decided not to change any provided example . If you wish to test the core, download the @ArduinoBot generated packages and modify the examples in this way:
On the top of the file: